//
//  TemplateFoundDetailsViewController.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/7/16.
//

#import "TemplateFoundDetailsViewController.h"
#import "TemplateFoundDetailsHeaderView.h"
#import "TemplateFoundDetailsFooterView.h"
#import "TemplateFoundDetailsDXXZTableViewCell.h"
#import "TemplateFoundDetailsDXXLTableViewCell.h"
#import "TemplateFoundDetailsSZSRTableViewCell.h"
#import "TemplateFoundDetailsDHWBTableViewCell.h"
#import "TemplateFoundDetailsDHTableViewCell.h"
#import "TemplateAddRWShowView.h"
#import "TaskFrequencyShowView.h"
#import "YTTipsShowView.h"

@interface TemplateFoundDetailsViewController ()
@property (nonatomic ,strong) UIScrollView * scrollview ;
@property (nonatomic ,strong) UIButton * tjBtn ;
@end

@implementation TemplateFoundDetailsViewController
- (void)setModel:(TemplateFoundDetailsModel *)model{
    _model = model;
    int i = 1;
    for (TemplateFoundDetailsStepModelListModel * listmodel in model.stepModelList) {
        listmodel.index = i;
        i++;
    }
    ///排序
    for (TemplateFoundDetailsStepModelListModel * listmodel in model.stepModelList) {///每个步骤
        for (TemplateFoundDetailsTaskTypeModelListModel * OldTaskTypeModel in listmodel.taskTypeModelList) {///每个任务
            if(OldTaskTypeModel.sourceFlag == 1){
                continue;
            }
            int index = 1;
            NSString * superName = @"";
            
            for (TemplateFoundDetailsTaskTypeModelListModel * NewTaskTypeModel in listmodel.taskTypeModelList) {///每个任务
                if([OldTaskTypeModel.commonId isEqualToString:NewTaskTypeModel.templateTasktypeId] || [OldTaskTypeModel.ID isEqualToString:NewTaskTypeModel.templateTasktypeId] || [OldTaskTypeModel.templateTasktypeId isEqualToString: NewTaskTypeModel.templateTasktypeId]){///ID一致的同名任务

                    if(NewTaskTypeModel.sourceFlag == 1){///来着公共模版,只要第一个是公共模版的就没有问题
                        superName = NewTaskTypeModel.name;
                        NewTaskTypeModel.nameStr = [DTKTool AttributedStringWithStr:[NSString stringWithFormat:@"%@",NewTaskTypeModel.name] andNodeColor:KcolorHex(@"#222222") andSeledColor:KcolorHex(@"#949494") andNodeFont:kCFont(17) andFont:kFont(19) andPattenr:@""];
                    } else {
                        if(superName.length < 1){
                            superName = [NewTaskTypeModel.name componentsSeparatedByString:@" "].firstObject;
                        }
//                        NewTaskTypeModel.name = [NSString stringWithFormat:@"%@ %d-%d",superName,listmodel.index,index];
                        NewTaskTypeModel.nameStr = [DTKTool AttributedStringWithStr:[NSString stringWithFormat:@"%@ %d-%d",NewTaskTypeModel.name,self.model.seledModel.index,index] andNodeColor:KcolorHex(@"#222222") andSeledColor:KcolorHex(@"#949494") andNodeFont:kCFont(17) andFont:kFont(19) andPattenr:[NSString stringWithFormat:@"(%d-%d)",listmodel.index,index]];

                        index++;
                    }
                }
            }
            
        }
    }
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.shutDownBack = YES;
    self.NoDataHidden = YES;
    [self setupView];
    // Do any additional setup after loading the view.
}
- (void)setupView{
    UIView * topview = [UIView new];
    topview.backgroundColor = KcolorHex(@"#0967FF");
    [self.view addSubview:topview];
    [topview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.height.offset(Knav_HeightX);
    }];
    MJWeakSelf
    UIView * backview = [UIView new].addClickBlock(^{
        [weakSelf popViewController];
    });
    [topview addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(44);
        make.left.bottom.offset(0);
        make.width.offset(kwt(170));
    }];
    
    UIImageView * leftimg = kimageView(@"white_path");
    [backview addSubview:leftimg];
    [leftimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(20);
        make.left.offset(kwt(10));
        make.centerY.offset(0);
    }];
    
    UILabel * leftlab = [DTKTool labelWithText:@"创建个人模版" andFont:kCFont(19) andTextColor:UIColor.whiteColor];
    [backview addSubview:leftlab];
    [leftlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(43));
        make.centerY.offset(0);
    }];
    if(self.model.templateName.length > 0){
        leftlab.text =self.model.name;
//        UILabel * title = [DTKTool labelWithText:self.model.name andFont:kCFont(19) andTextColor:UIColor.whiteColor];
//        [topview addSubview:title];
//        title.textAlignment = NSTextAlignmentCenter;
//        [title mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.centerX.offset(0);
//            make.width.offset(kwt(250));
//            make.centerY.equalTo(backview);
//        }];
    }
    
    UIScrollView * scrollview = [UIScrollView new];
    self.scrollview = scrollview;
    scrollview.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:scrollview];
    [scrollview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(topview.mas_bottom);
        make.height.offset(kwt(46));
    }];
    
    UIView * buttomview = [UIView new];
    buttomview.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:buttomview];
    [buttomview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.offset(kwt(63)+ KSafeAreaBottomHeight);
    }];
    
    UIView * addGZview = [UIView new];
    addGZview.addClickBlock(^{
        [weakSelf addGZbtnClick];
    });
    [buttomview addSubview:addGZview];
    [addGZview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.offset(0);
        make.height.offset(kwt(63));
        make.width.offset(kwt(105));
    }];
    
    UIImageView * gzimg = kimageView(@"template_new_job");
    [addGZview addSubview:gzimg];
    [gzimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.offset(kwt(10));
        make.width.height.offset(kwt(20));
    }];
    
    UILabel * gzlab = [DTKTool labelWithText:@"新增工作" andFont:kCFont(16) andTextColor:KcolorHex(@"#222222")];
    [addGZview addSubview:gzlab];
    [gzlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.offset(kwt(37));
    }];
    
    UIButton * tjBtn = [UIButton buttonWithType:0];
    self.tjBtn = tjBtn;
    tjBtn.addNormalText(@"下一步").addNormalColor(UIColor.whiteColor).addFont(kCFont(18)).addbackgroundColor([UIColor gradientColorWithSize:CGSizeMake(kwt(260), 1) direction:GradientColorDirectionLevel startColor:KcolorHex(@"#427FFF") endColor:KcolorHex(@"#3D5FFF")]).addTarget(self,@selector(TJBtnClick));
    [DTKTool YJ:kwt(10) andView:tjBtn];
    [buttomview addSubview:tjBtn];
    [tjBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(kwt(260));
        make.height.offset(kwt(48));
        make.right.offset(kwt(-10));
        make.top.offset(kwt(7.5));
    }];
    
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(scrollview.mas_bottom);
        make.bottom.equalTo(buttomview.mas_top);
    }];
    ///默认处理
    [self.model seledModel];
    [self RefreshUI];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.model.seledModel.taskTypeModelList.count;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    TemplateFoundDetailsHeaderView * headerview = [TemplateFoundDetailsHeaderView NewView];
    headerview.model = self.model.seledModel.taskTypeModelList[section];
    //    [headerview setIndex:section + 1 andSourceFlagName:[NSString stringWithFormat:@"%@ %d",self.model.seledModel.taskTypeModelList[section].name,[self.model.seledModel ItemModelListIndexWithmodel:self.model.seledModel.taskTypeModelList[section]]]];
    [headerview setIndex:section + 1 andSourceFlagName:self.model.seledModel.taskTypeModelList[section].nameStr];
    MJWeakSelf
    ///选频次
    headerview.openRWPCBlock = ^(TemplateFoundDetailsTaskTypeModelListModel * _Nonnull Model) {
        [TaskFrequencyShowView showviewWithType:Model.taskType andIndexs:@[[NSNumber numberWithInteger:Model.rateDay - 1],[NSNumber numberWithInteger:Model.rateNumber - 1]] andBlock:^(NSInteger taskType, NSInteger rateDay, NSInteger rateNumber) {
            Model.taskType = taskType;
            Model.rateDay = rateDay;
            Model.rateNumber = rateNumber;
            [Model TimeArrayReload];
            [weakSelf.tableview reloadData];
        }];
        //        NSMutableArray * brmodelArray = [NSMutableArray array];
        //        for (int i = 1; i < 100; i++) {
        //            BRResultModel * rm = [BRResultModel new];
        //            rm.key = [NSString stringWithFormat:@"F%d",i];
        //            rm.value = [NSString stringWithFormat:@"%d天",i];
        //            rm.parentKey = @"-1";
        //            rm.parentValue = @"";
        //            rm.remark = [NSString stringWithFormat:@"%d",i];
        //            [brmodelArray addObject:rm];
        //            BRResultModel * dayrm = [BRResultModel new];
        //            dayrm.key = [NSString stringWithFormat:@"Z%d",i];
        //            dayrm.value = @"1次";
        //            dayrm.parentKey = rm.key;
        //            dayrm.parentValue = rm.value;
        //            dayrm.remark = [NSString stringWithFormat:@"1"];
        //            [brmodelArray addObject:dayrm];
        //        }
        //        for (int i = 2; i < 100; i++) {
        //            BRResultModel * rm = [BRResultModel new];
        //            rm.key = [NSString stringWithFormat:@"Z%d",i];
        //            rm.value = [NSString stringWithFormat:@"%d次",i];
        //            rm.parentKey = @"F1";
        //            rm.parentValue = @"1";
        //            rm.remark = [NSString stringWithFormat:@"%d",i];
        //
        //            [brmodelArray addObject:rm];
        //        }
        //        ///任务频次
        //        [BRStringPickerView showLinkagePickerWithTitle:@"选择任务频次" dataSourceArr:brmodelArray selectIndexs:@[[NSNumber numberWithInteger:Model.rateDay - 1],[NSNumber numberWithInteger:Model.rateNumber - 1]] isAutoSelect:NO resultBlock:^(NSArray<BRResultModel *> * _Nullable resultModelArr) {
        //            Model.rateDay = [resultModelArr.firstObject.remark integerValue];
        //            Model.rateNumber = [resultModelArr.lastObject.remark integerValue];
        //            [Model TimeArrayReload];
        //            [weakSelf.tableview reloadData];
        //        }];
    };
    headerview.openRWTimeBlock = ^(TemplateFoundDetailsExecuteModelListModel * _Nonnull Model) {
        [BRDatePickerView showDatePickerWithMode:BRDatePickerModeHM title:@"请选择任务执行时间" selectValue:Model.executeTime resultBlock:^(NSDate * _Nullable selectDate, NSString * _Nullable selectValue) {
            for (TemplateFoundDetailsExecuteModelListModel * oldModel in weakSelf.model.seledModel.taskTypeModelList[section].executeModelList) {
                if([oldModel.executeTime isEqualToString:selectValue]){
                    [weakSelf.view showWarning:@"已有该时间,请重新选择"];
                    return;
                }
            }
            Model.executeTime = selectValue;
            [weakSelf.model.seledModel.taskTypeModelList[section] TimeArrayReload];
            [weakSelf.tableview reloadData];
        }];
    };
    headerview.openReloadBlock = ^{
        [weakSelf.tableview reloadData];
    };
    headerview.openRemoveBlock = ^(TemplateFoundDetailsTaskTypeModelListModel * _Nonnull Model) {
        [YTTipsShowView showviewWithTips:@"确定删除该任务吗" andRightBlock:^{
            [weakSelf.model.seledModel.taskTypeModelList removeObject:Model];
            [weakSelf modelNewDate];
            [weakSelf.tableview reloadData];
        }];
    };
    return headerview;
}
- (void)modelNewDate{
    for (TemplateFoundDetailsTaskTypeModelListModel * NewTaskTypeModel in self.model.seledModel.taskTypeModelList) {
        NSString * superName = @"";
        int i = 1;
        for (TemplateFoundDetailsTaskTypeModelListModel * OldTaskTypeModel in self.model.seledModel.taskTypeModelList) {
            
            if([OldTaskTypeModel.commonId isEqualToString:NewTaskTypeModel.ID] || [OldTaskTypeModel.ID isEqualToString:NewTaskTypeModel.ID] || [OldTaskTypeModel.templateTasktypeId isEqualToString: NewTaskTypeModel.ID]){///ID一致的同名任务
                if(OldTaskTypeModel.sourceFlag == 1){///来着公共模版,只要第一个是公共模版的就没有问题
                    superName = OldTaskTypeModel.name;
                } else {
                    if(superName.length < 1){///母版没有
                        superName = NewTaskTypeModel.name;
                        OldTaskTypeModel.nameStr = [DTKTool AttributedStringWithStr:[NSString stringWithFormat:@"%@ %d-%d",NewTaskTypeModel.name,self.model.seledModel.index,i] andNodeColor:KcolorHex(@"#222222") andSeledColor:KcolorHex(@"#949494") andNodeFont:kCFont(17) andFont:kFont(19) andPattenr:[NSString stringWithFormat:@"(%d-%d)",self.model.seledModel.index,i]];
                    } else {
                        OldTaskTypeModel.nameStr = [DTKTool AttributedStringWithStr:[NSString stringWithFormat:@"%@ %d-%d",superName,self.model.seledModel.index ,i] andNodeColor:KcolorHex(@"#222222") andSeledColor:KcolorHex(@"#949494") andNodeFont:kCFont(17) andFont:kFont(19) andPattenr:[NSString stringWithFormat:@"(%d-%d)",self.model.seledModel.index,i]];
                    }
                    i++;
                }
            
            }
            
        }
      
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return self.model.seledModel.taskTypeModelList[section].haederViewH;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    TemplateFoundDetailsFooterView * view = [TemplateFoundDetailsFooterView NewView];
    view.model = self.model.seledModel.taskTypeModelList[section];
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return self.model.seledModel.taskTypeModelList[section].footerViewH;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(self.model.seledModel.taskTypeModelList[section].isSQ){
        return 0;
    }
    return self.model.seledModel.taskTypeModelList[section].taskItemModelList.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.model.seledModel.taskTypeModelList[indexPath.section].taskItemModelList[indexPath.row].CellH;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    MJWeakSelf
    TemplateFoundDetailsTaskItemModelListModel * model = self.model.seledModel.taskTypeModelList[indexPath.section].taskItemModelList[indexPath.row];
    if(model.pattern == 1) {///多选
        TemplateFoundDetailsDXXZTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"TemplateFoundDetailsDXXZTableViewCell"];
        if (cell == nil) {
            cell = [[TemplateFoundDetailsDXXZTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TemplateFoundDetailsDXXZTableViewCell"];
        }
        cell.model = model;
        cell.openReloadBlock = ^{
            [weakSelf.tableview reloadData];
        };
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell ;
        
    } else if( model.pattern == 2){///单选
        TemplateFoundDetailsDXXLTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"TemplateFoundDetailsDXXLTableViewCell"];
        if (cell == nil) {
            cell = [[TemplateFoundDetailsDXXLTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TemplateFoundDetailsDXXLTableViewCell"];
        }
        cell.model = model;
        ///下拉单选
        cell.openXLDXBlock = ^(TemplateFoundDetailsTaskItemModelListModel * _Nonnull Model, TemplateFoundDetailsItemModelListModel * _Nonnull itemModel) {
            NSMutableArray * nameArray = [NSMutableArray array];
            int i = 0;
            int selectIndex = 0;
            for (TemplateFoundDetailsItemModelListModel * itemmodel in Model.itemModelList) {
                [nameArray addObject:itemmodel.name];
                if(itemmodel == itemModel){
                    selectIndex = i;
                }
                i ++;
            }
            [BRStringPickerView showPickerWithTitle:[NSString stringWithFormat:@"请选择%@",model.name] dataSourceArr:nameArray selectIndex:selectIndex resultBlock:^(BRResultModel * _Nullable resultModel) {
                for (TemplateFoundDetailsItemModelListModel * itemmodel in Model.itemModelList) {
                    if([itemmodel.name isEqualToString:resultModel.value]){
                        itemmodel.checkedFlag = 1;
                    } else {
                        itemmodel.checkedFlag = 0;
                    }
                }
                [weakSelf.tableview reloadData];
            }];
        };    cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell ;
    } else if(model.pattern == 3){///数字输入
        
        TemplateFoundDetailsSZSRTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"TemplateFoundDetailsSZSRTableViewCell"];
        if (cell == nil) {
            cell = [[TemplateFoundDetailsSZSRTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TemplateFoundDetailsSZSRTableViewCell"];
        }
        cell.model = model;
        cell.openReloadBlock = ^{
            [weakSelf.tableview reloadData];
        };
        cell.openHUDBlock = ^(NSString * _Nonnull str) {
            [weakSelf.view showWarning:str];
            [weakSelf.tableview reloadData];
        };
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell ;
    } else if(model.pattern == 4){///单行文本
        TemplateFoundDetailsDHWBTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"TemplateFoundDetailsDHWBTableViewCell"];
        if (cell == nil) {
            cell = [[TemplateFoundDetailsDHWBTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TemplateFoundDetailsDHWBTableViewCell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = model;
        return cell ;
    } else if(model.pattern == 5){///多行文本
        
        TemplateFoundDetailsDHTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"TemplateFoundDetailsDHTableViewCell"];
        if (cell == nil) {
            cell = [[TemplateFoundDetailsDHTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TemplateFoundDetailsDHTableViewCell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = model;
        return cell ;
    }
    
    UITableViewCell * cell =  [UITableViewCell new];
    return cell ;
}



///点击添加任务弹窗
- (void)addGZbtnClick{
    NSString * ID;
    if(self.model.templateId.length > 0){
        ID = self.model.templateId;
    } else {
        ID = self.model.ID;
    }
    MJWeakSelf
    [TemplateAddRWShowView showviewWithId:ID andRightBlock:^(NSArray<NSString *> * _Nonnull IDArray) {
        [weakSelf addRWIDArray:IDArray];
    }];
}
///添加任务
- (void)addRWIDArray:(NSArray<NSString *> * )IDArray{
    NSMutableArray * array = [NSMutableArray array];
    for (NSString * ID in IDArray) {
        [array addObject:@{@"id":ID}];
    }
    [self.view showBusyHUD];
    [SCNetManger PostMBselectTypeWithDict:array andCompetionHandlerBlock:^(HTTPArrBase * model) {
        [self.view hideBusyHUD];
        if(model.code == 200){
            NSArray * taskTypeModelList = [NSArray yy_modelArrayWithClass:TemplateFoundDetailsTaskTypeModelListModel.class json:model.result];
            //            for (TemplateFoundDetailsTaskTypeModelListModel * m in taskTypeModelList) {
            //                m.ID = m.templateTasktypeId;
            //                for (TemplateFoundDetailsTaskItemModelListModel * listmodel in m.taskItemModelList) {
            //                    listmodel.ID = listmodel.templateTaskitemId;
            //                    for (TemplateFoundDetailsItemModelListModel * itemmodel in listmodel.itemModelList) {
            //                        itemmodel.ID = itemmodel.templateItemId;
            //                    }
            //                }
            //            }
            
            ///排序
//            for (TemplateFoundDetailsTaskTypeModelListModel * NewTaskTypeModel in taskTypeModelList) {
//                int i = 1;
//                NSString * superName = @"";
//                for (TemplateFoundDetailsTaskTypeModelListModel * OldTaskTypeModel in self.model.seledModel.taskTypeModelList) {
//                    if([OldTaskTypeModel.templateTasktypeId isEqualToString:NewTaskTypeModel.templateTasktypeId] || [OldTaskTypeModel.ID isEqualToString:NewTaskTypeModel.templateTasktypeId]){///ID一致的同名任务
//                        if(OldTaskTypeModel.sourceFlag == 1){///来着公共模版,只要第一个是公共模版的就没有问题
//                            superName = OldTaskTypeModel.name;
//                        } else {
//                            if(superName.length < 1){///母版没有
//                                superName = NewTaskTypeModel.name;
////                                OldTaskTypeModel.name = [NSString stringWithFormat:@"%@ %d-%d",NewTaskTypeModel.name,self.model.seledModel.index,i];
//                                OldTaskTypeModel.nameStr = [DTKTool AttributedStringWithStr:[NSString stringWithFormat:@"%@ %d-%d",superName,self.model.seledModel.index,i] andNodeColor:KcolorHex(@"#222222") andSeledColor:KcolorHex(@"#949494") andNodeFont:kCFont(17) andFont:kFont(19) andPattenr:[NSString stringWithFormat:@"(%d-%d)",self.model.seledModel.index,i]];
//
//                            } else {
////                                OldTaskTypeModel.name = [NSString stringWithFormat:@"%@ %d-%d",superName,self.model.seledModel.index,i];
//                            }
//                            i++;
//                        }
//                    }
//                    if(superName.length < 1){///母版没有
////                        superName = NewTaskTypeModel.name;
////                        NewTaskTypeModel.name = [NSString stringWithFormat:@"%@ %d-%d",NewTaskTypeModel.name,self.model.seledModel.index,i];
//                    } else {
////                        NewTaskTypeModel.name = [NSString stringWithFormat:@"%@ %d-%d",superName,self.model.seledModel.index,i];
//
//                    }
//                }
//            }
            for (TemplateFoundDetailsTaskTypeModelListModel * NewTaskTypeModel in taskTypeModelList) {
                int i = 1;
                NSString * superName = @"";
                for (TemplateFoundDetailsTaskTypeModelListModel * OldTaskTypeModel in self.model.seledModel.taskTypeModelList) {
                    if([OldTaskTypeModel.commonId isEqualToString:NewTaskTypeModel.ID] || [OldTaskTypeModel.ID isEqualToString:NewTaskTypeModel.ID] || [OldTaskTypeModel.templateTasktypeId isEqualToString: NewTaskTypeModel.ID]){///ID一致的同名任务
                        if(OldTaskTypeModel.sourceFlag == 1){///来着公共模版,只要第一个是公共模版的就没有问题
                            superName = OldTaskTypeModel.name;
                        } else {
                            if(superName.length < 1){///母版没有
                                superName = NewTaskTypeModel.name;
                                OldTaskTypeModel.nameStr = [DTKTool AttributedStringWithStr:[NSString stringWithFormat:@"%@ %d-%d",NewTaskTypeModel.name,self.model.seledModel.index,i] andNodeColor:KcolorHex(@"#222222") andSeledColor:KcolorHex(@"#949494") andNodeFont:kCFont(17) andFont:kFont(19) andPattenr:[NSString stringWithFormat:@"(%d-%d)",self.model.seledModel.index,i]];
                            } else {
//                                OldTaskTypeModel.name = superName;
                                OldTaskTypeModel.nameStr = [DTKTool AttributedStringWithStr:[NSString stringWithFormat:@"%@ %d-%d",superName,self.model.seledModel.index,i] andNodeColor:KcolorHex(@"#222222") andSeledColor:KcolorHex(@"#949494") andNodeFont:kCFont(17) andFont:kFont(19) andPattenr:[NSString stringWithFormat:@"(%d-%d)",self.model.seledModel.index,i]];
                            }
                            i++;
                        }
                    }
                    if(superName.length < 1){///母版没有
                        superName = NewTaskTypeModel.name;
//                        NewTaskTypeModel.name = [NSString stringWithFormat:@"%@ %d-%d",NewTaskTypeModel.name,self.model.seledModel.index,i];
                        NewTaskTypeModel.nameStr = [DTKTool AttributedStringWithStr:[NSString stringWithFormat:@"%@ %d-%d",NewTaskTypeModel.name,self.model.seledModel.index,i] andNodeColor:KcolorHex(@"#222222") andSeledColor:KcolorHex(@"#949494") andNodeFont:kCFont(17) andFont:kFont(19) andPattenr:[NSString stringWithFormat:@"(%d-%d)",self.model.seledModel.index,i]];
                    } else {
//                        NewTaskTypeModel.name = superName;
                        NewTaskTypeModel.nameStr = [DTKTool AttributedStringWithStr:[NSString stringWithFormat:@"%@ %d-%d",superName,self.model.seledModel.index ,i] andNodeColor:KcolorHex(@"#222222") andSeledColor:KcolorHex(@"#949494") andNodeFont:kCFont(17) andFont:kFont(19) andPattenr:[NSString stringWithFormat:@"(%d-%d)",self.model.seledModel.index,i]];
                    }
                }
            }
            for (TemplateFoundDetailsTaskTypeModelListModel * m in taskTypeModelList) {
                m.templateTasktypeId = m.ID;
                m.ID = @"";
                for (TemplateFoundDetailsTaskItemModelListModel * listmodel in m.taskItemModelList) {
                    listmodel.templateTaskitemId = listmodel.ID;
                    listmodel.ID = @"";
                    for (TemplateFoundDetailsItemModelListModel * itemmodel in listmodel.itemModelList) {
                        itemmodel.templateItemId = itemmodel.ID;
                        itemmodel.ID = @"";
                        for (TemplateFoundDetailsItemListModel * itemlistModel in itemmodel.itemList) {
                            itemlistModel.templateTaskitemId = itemlistModel.ID;
                            itemlistModel.ID = @"";
                            for (TemplateFoundDetailsChildrenModelListModel * childmodel in itemlistModel.childrenModelList) {
                                childmodel.templateItemId = childmodel.ID;
                                childmodel.ID = @"";
                            }
                        }
                    }
                }
            }
            [self.model.seledModel.taskTypeModelList addObjectsFromArray:taskTypeModelList];
            
            [self.tableview reloadData];
        } else {
            [self.view showWarning:model.message];
        }
    }];
}

- (void)TJBtnClick{
    if(self.model.seledModel != self.model.stepModelList.lastObject){
        [self.model setSeledModel:self.model.stepModelList[[self.model.stepModelList indexOfObject:self.model.seledModel] + 1]];
        [self RefreshUI];
    } else {
        ///提交
        [self TJAPI];
    }
    
}
- (void)TJAPI{
    
    if([self.model TjAPiHUDString].length > 0){
        [self.view showWarning:[self.model TjAPiHUDString]];
        return;
    }
    [self.view showBusyHUD];
    for (TemplateFoundDetailsStepModelListModel * stepmodel in self.model.stepModelList) {///步骤
        for (TemplateFoundDetailsTaskTypeModelListModel * takemodel in stepmodel.taskTypeModelList) {///任务
            for (TemplateFoundDetailsTaskItemModelListModel * itemmodel in takemodel.taskItemModelList) {///任务项
                for (TemplateFoundDetailsItemModelListModel * itemModelList in itemmodel.itemModelList) {///任务选项
                    ///清空多余数据
                    [itemModelList.itemList removeAllObjects];
                }
            }
        }
    }
    
    NSMutableDictionary * dict = [self.model TjDict];
    
    
    if(self.model.templateId.length > 0){///修改
        [SCNetManger PostTemplateEditWithDict:dict andCompetionHandlerBlock:^(HTTPArrBase * model) {
            [self.view hideBusyHUD];
            NSLog(@"%@",dict);
            if(model.code == 200){
                [self.navigationController popViewControllerAnimated:YES];
                [self.navigationController popViewControllerAnimated:YES];
            } else {
                [self.view showWarning:model.message];
            }
        }];
    } else{///新增
        [SCNetManger PostTemplateAddWithDict:dict andCompetionHandlerBlock:^(HTTPArrBase * model) {
            [self.view hideBusyHUD];
            NSLog(@"%@",dict);
            if(model.code == 200){
                [self.view showWarning:model.message];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    if(self.overBlock){
                        self.overBlock();
                    }
                    // 2秒后执行的代码
                    [self.navigationController popViewControllerAnimated:YES];
                    [self.navigationController popViewControllerAnimated:YES];

                });
            } else {
                [self.view showWarning:model.message];
            }
        }];
    }
    
    
    
}

///刷新UI
- (void)RefreshUI{
    [self.scrollview.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    CGFloat X = 0;

    if(self.model.stepModelList.count > 1){
        CGFloat maxW = 0;
        for (TemplateFoundDetailsStepModelListModel * bzModel in self.model.stepModelList) {//步骤
            maxW += bzModel.nameW + kwt(1);
        }
        CGFloat btnAddWidth = 0;
        if(maxW < SCREEN_WIDTH + kwt(1)){///需要填充间隙
            btnAddWidth = (SCREEN_WIDTH + kwt(1) - maxW) / self.model.stepModelList.count;
        }
        int index = 1;
        MJWeakSelf
        for (TemplateFoundDetailsStepModelListModel * bzModel in self.model.stepModelList) {//步骤
            UIView * btnview = [UIView new];
            if(bzModel.isSeled){
                btnview.backgroundColor = UIColor.whiteColor;
                UILabel * leftlab = [DTKTool labelWithText:[NSString stringWithFormat:@"%d",index] andFont:kCFont(13) andTextColor:KcolorHex(@"#2C72F7")];
                [DTKTool YJBK:kwt(4) andView:leftlab andWidth:1 andColor:KcolorHex(@"#2C72F7")];
                leftlab.textAlignment = NSTextAlignmentCenter;
                [btnview addSubview:leftlab];
                [leftlab mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.offset(kwt(12) + btnAddWidth/2);
                    make.centerY.offset(0);
                    make.width.height.offset(kwt(18));
                }];
                UILabel * rightlab = [DTKTool labelWithText:bzModel.name andFont:kCFont(17) andTextColor:KcolorHex(@"#222222")];
                [btnview addSubview:rightlab];
                [rightlab mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.offset(kwt(34)+ btnAddWidth/2);
                    make.centerY.offset(0);
                }];
                
                
            } else{
                btnview.backgroundColor = KcolorHex(@"#ECF1FD");
                [DTKTool BFYJ:kwt(10) andView:btnview andCorner:UIRectCornerBottomLeft|UIRectCornerBottomRight andSize:CGRectMake(0, 0, bzModel.nameW + btnAddWidth, kwt(46))];
                
                UILabel * leftlab = [DTKTool labelWithText:[NSString stringWithFormat:@"%d",index] andFont:kCFont(13) andTextColor:KcolorHex(@"#8A8A8A")];
                [DTKTool YJBK:kwt(4) andView:leftlab andWidth:1 andColor:KcolorHex(@"#8A8A8A")];
                leftlab.textAlignment = NSTextAlignmentCenter;
                [btnview addSubview:leftlab];
                [leftlab mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.offset(kwt(12)+ btnAddWidth/2);
                    make.centerY.offset(0);
                    make.width.height.offset(kwt(18));
                }];
                
                UILabel * rightlab = [DTKTool labelWithText:bzModel.name andFont:kFont(17) andTextColor:KcolorHex(@"#222222")];
                [btnview addSubview:rightlab];
                [rightlab mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.offset(kwt(34)+ btnAddWidth/2);
                    make.centerY.offset(0);
                }];
                
            }
            btnview.addClickBlock(^{
                [weakSelf.model setSeledModel:bzModel];
                [weakSelf RefreshUI];
            });
            btnview.frame = CGRectMake(X, 0, bzModel.nameW + btnAddWidth , kwt(46));
            [self.scrollview addSubview:btnview];
            X += bzModel.nameW + kwt(1) + btnAddWidth;
            index += 1;
        }
    } else {
        [self.scrollview mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.offset(0);
        }];
    }
    
    self.scrollview.contentSize = CGSizeMake(X, 0);
    [self.tableview reloadData];
    self.tjBtn.addNormalText(self.model.isLastModel?@"提交":@"下一步");
}


@end
